from src import app, CustomBlueprint
from src.baseapp.controller.planService.planController import PlanController
from flask import Response
import json

from src.baseapp.requestVerify.BaseRequestVerify import *
from src.baseapp.requestVerify.planService.planRequestVerify import *

# 设置这个模块的路由前缀
plan_bp = CustomBlueprint('planService', __name__, url_prefix='/planService')


@plan_bp.route('/getList', methods=['GET'], header_verify=HeadersSchema, body_verify=PlanListBodySchema)
def getPlanList(request_obj):
    with PlanController() as plan:
        result = plan.get_plan_list(request_obj.body.to_dict)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@plan_bp.route('/plan', methods=['GET'], header_verify=HeadersSchema, body_verify=PlanInfoBodySchema)
def get_plan_info(request_obj):
    with PlanController() as plan:
        result = plan.get_plan_info(request_obj.body.plan_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@plan_bp.route('/plan', methods=['POST'], header_verify=HeadersSchema, body_verify=PlanCreateBodySchema)
def create_plan(request_obj):
    with PlanController() as plan:
        result = plan.add_plan(request_obj.body.to_dict)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@plan_bp.route('/plan', methods=['DELETE'], header_verify=HeadersSchema, body_verify=PlanInfoBodySchema)
def delete_plan(request_obj):
    with PlanController() as plan:
        result = plan.delete_plan(request_obj.body.plan_id)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@plan_bp.route('/plan', methods=['PUT'], header_verify=HeadersSchema, body_verify=PlanUpdateBodySchema)
def update_plan(request_obj):
    plan_info = request_obj.body.to_dict
    plan_ids = plan_info.pop('plan_ids')

    with PlanController() as plan:
        result = plan.update_plan(plan_ids, plan_info)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@plan_bp.route('/addCase', methods=['POST'], header_verify=HeadersSchema, body_verify=PlanCaseBodySchema)
def add_case_to_plan(request_obj):
    with PlanController() as plan:
        result = plan.add_testcase_to_plan(request_obj.body.plan_id, request_obj.body.case_ids)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@plan_bp.route('/removeCase', methods=['DELETE'], header_verify=HeadersSchema, body_verify=PlanCaseBodySchema)
def remove_case_from_plan(request_obj):
    with PlanController() as plan:
        result = plan.remove_testcase_from_plan(request_obj.body.plan_id, request_obj.body.case_ids)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


@plan_bp.route('/updateCaseStatus', methods=['PUT'], header_verify=HeadersSchema, body_verify=PlanCaseStatusBodySchema)
def update_case_status(request_obj):
    with PlanController() as plan:
        result = plan.update_testcase_status(request_obj.body.plan_id, request_obj.body.case_ids,
                                             request_obj.body.status, request_obj.body.remark)
        resp = Response(json.dumps((result[1])), status=result[0], mimetype='application/json')
    return resp


app.register_blueprint(plan_bp)
